import type { PageConfig, PageConfigWithPath } from '@/types/page-config'

// 动态导入所有页面配置文件
const configModules = import.meta.glob('../views/**/config.ts', { eager: true })

/**
 * 获取所有页面配置
 */
export const getAllPageConfigs = (): PageConfigWithPath[] => {
  const configs: PageConfigWithPath[] = []

  for (const path in configModules) {
    const module = configModules[path] as { default: PageConfig }
    const config = module.default

    // 提取文件路径信息
    const pathMatch = path.match(/\.\.\/views\/(.+)\/config\.ts$/)
    if (!pathMatch) continue

    const filePath = pathMatch[1]
    
    // 生成路由路径
    let routePath = '/' + filePath.toLowerCase()
    
    configs.push({
      ...config,
      path: routePath,
      filePath: filePath
    })
  }

  return configs.sort((a, b) => (a.order || 999) - (b.order || 999))
}

/**
 * 根据路径获取页面配置
 */
export const getPageConfigByPath = (path: string): PageConfigWithPath | undefined => {
  const configs = getAllPageConfigs()
  return configs.find(config => config.path === path)
}

/**
 * 获取菜单显示的页面配置
 */
export const getMenuConfigs = (): PageConfigWithPath[] => {
  return getAllPageConfigs().filter(config => config.showInMenu !== false)
}

/**
 * 根据分组获取页面配置
 */
export const getConfigsByGroup = (group: string): PageConfigWithPath[] => {
  return getAllPageConfigs().filter(config => config.group === group)
}